import table from '@/components/dt/table/render.js'
import Opt from '@/components/dt/info/Opt.js'
import dateOpt from '@/biz/option/date.js'
import tool from "@/common/tool.js"
let _this = null

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    console.log(_this.curState, _this.orderType, 'curState')
    this.all = []
    let publicArr = [{
      title: `${_this.orderType}调整单编号`,
      field: 'adjustNo',
      minWidth: 160,
      header: {
        filter: {
          title: `${_this.orderType}调整单编号`,
          key: 'adjustNo',
          placeholder: `请输入至少4位${_this.orderType}单编号`,
          input: {},
          check: (val) => {
            if (val.adjustNo.length < 4) {
              _this.$Message.warning(`请输入至少4位${_this.orderType}单编号。`)
            }
            return !(val.adjustNo.length < 4)
          }
        },
      },
    },
    {
      title: '所属组织',
      field: 'orgName',
      minWidth: 130
    },
    {
      title: '所属案场',
      field: 'housesName',
      minWidth: 150
    },
    {
      title: '调整订单数',
      field: 'adjustCount',
      minWidth: 120
    },
    {
      title: '调整前总面积(㎡)',
      field: 'beforeAdjustArea',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: `调整前${_this.orderType}总价(元)`,
      field: 'beforeAdjustPrice',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: '调整后总面积(㎡)',
      field: 'afterAdjustArea',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: `调整后${_this.orderType}总价(元)`,
      field: 'afterAdjustPrice',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      title: '调整面积差(㎡)',
      field: 'areaDV',
      minWidth: 120,
      slots: {
        default: (params, h) => {
          let color = params.row.areaDV > 0 ? '#D9001B' : params.row
            .areaDV < 0 ?
            '#70B603' : '#515a6e'
          let areaDVTmp = params.row.areaDV > 0 ? '+' + params.row.areaDV : params.row
            .areaDV < 0 ? params.row.areaDV : params.row.areaDV
          return table.cell.text(h, {
            value: `<span style=color:${color};margin-left:5px>${areaDVTmp}</span>`,
            isHtml: true,
            tipsTxt: params.row.areaDV
          })
        }
      }
    },
    {
      title: '调整总价差(元)',
      field: 'priceDV',
      minWidth: 120,
      slots: {
        default: (params, h) => {
          let color = params.row.priceDV > 0 ? '#D9001B' : params.row
            .priceDV < 0 ?
            '#70B603' : '#515a6e'
          let priceDVTmp = params.row.priceDV > 0 ? '+' + params.row.priceDV :
            params.row.priceDV < 0 ? params.row.priceDV : params.row.priceDV
          return table.cell.text(h, {
            value: `<span style=color:${color};margin-left:5px>${priceDVTmp}</span>`,
            isHtml: true,
            tipsTxt: params.row.priceDV
          })
        }
      }
    }, ]
    let p1 = [{
      title: '调整后新增回款笔数',
      field: 'totalBackNum',
      minWidth: 160
    },
    {
      title: '正回款笔数/金额',
      field: 'plusCollectionPrice',
      minWidth: 140,
      slots: {
        default: (params, h) => {
          let color = params.row.plusCollectionPrice > 0 ? '#D9001B' : params.row
            .plusCollectionPrice < 0 ?
            '#70B603' : '#515a6e'
          let price =
            params.row.plusCollectionCount > 0 ? '(+' + params.row
            .plusCollectionPrice + ')' : ''
          return table.cell.text(h, {
            value: `${params.row.plusCollectionCount}<span style=color:${color};margin-left:5px>${price}</span>`,
            isHtml: true,
            tipsTxt: params.row.plusCollectionPrice
          })
        }
      }
    },
    {
      title: '负回款笔数/金额',
      field: 'minusCollectionPrice',
      minWidth: 140,
      slots: {
        default: (params, h) => {
          let color = params.row.minusCollectionPrice > 0 ? '#D9001B' : params.row
            .minusCollectionPrice < 0 ?
            '#70B603' : '#515a6e'
          let price =
            params.row.minusCollectionCount > 0 ? '(' + params.row
            .minusCollectionPrice + ')' : ''
          return table.cell.text(h, {
            value: `${params.row.minusCollectionCount}<span style=color:${color};margin-left:5px>${price} 
              </span>`,
            isHtml: true,
            tipsTxt: params.row.minusCollectionPrice
          })
        }
      }
    }, ]
    let p2 = [{
      title: '调整原因',
      field: 'adjustmentReason',
      minWidth: 220
    },
    {
      title: '调整附件',
      field: 'attachNum',
      minWidth: 100,
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: params.row.attachNum || '-',
            event: 'viewAccessory',
            data: params.row
          })
        }
      }
    },
    {
      title: '申请日期',
      field: 'submitTime',
      minWidth: 150,
      header: {
        filter: dateOpt.range({
          title: '申请日期',
          startKey: 'submitTimeStartDate',
          endKey: 'submitTimeEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (tool.dateDiff(val.submitTimeStartDate, val.submitTimeEndDate).Y >=
              2) {
              Message.warning('申请日期跨度至多两年。')
            }
            return !(tool.dateDiff(val.submitTimeStartDate, val.submitTimeEndDate)
              .Y >= 2)
          }
        }),
      },
      slots: {
        default: (params, h) => {
          return params.row.submitTime ? params.row.submitTime.split(' ')[0] : ''
        }
      }
    }, {
      title: '申请人',
      field: 'submitName',
      minWidth: 180
    }, ]

    let waitAuditArr = [{
      title: '操作',
      fixed: 'right',
      minWidth: 150,
      align: 'center',
      slots: {
        default: (params, h) => {
          let btns = []
          btns.push({
            title: '查看',
            event: 'viewFun',
            data: params.row
          })
          btns.push({
            title: (_this.orderType == '认购' && _this.unlocking('0206090101')) ||
              (_this.orderType == '签约' && _this.unlocking('0206090201')) ?
              '审核' : '',
            event: 'auditFun',
            data: params.row
          })
          return table.cell.btns(_this, h, btns)
        }
      }
    }]
    let passAuditArr = [
    {
      title: '审核日期',
      field: 'auditTime',
      minWidth: 180,
      header: {
        filter: dateOpt.range({
          title: '审核日期',
          startKey: 'auditTimeStartDate',
          endKey: 'auditTimeEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (tool.dateDiff(val.auditTimeStartDate, val.auditTimeEndDate).Y >=
              2) {
              Message.warning('申请日期跨度至多两年。')
            }
            return !(tool.dateDiff(val.auditTimeStartDate, val.auditTimeEndDate)
              .Y >= 2)
          }
        }),
      },
      slots: {
        default: (params, h) => {
          return params.row.auditTime ? params.row.auditTime.split(' ')[0] : ''
        }
      }
    },
    {
      title: '审核人',
      field: 'auditName',
      minWidth: 120
    },
    {
      title: '操作',
      fixed: 'right',
      minWidth: 150,
      align: 'center',
      slots: {
        default: (params, h) => {
          let btns = []
          btns.push({
            title: '查看',
            event: 'viewFun',
            data: params.row
          })
          btns.push({
            title: '流程信息',
            event: 'processFun',
            data: params.row
          })
          return table.cell.btns(_this, h, btns)
        }
      }
    }]
    let rejectAuditArr = [
    {
      title: '驳回原因',
      field: 'auditReason',
      minWidth: 220
    },
    {
      title: '审核日期',
      field: 'auditTime',
      minWidth: 180,
      header: {
        filter: dateOpt.range({
          title: '审核日期',
          startKey: 'auditTimeStartDate',
          endKey: 'auditTimeEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (tool.dateDiff(val.auditTimeStartDate, val.auditTimeEndDate).Y >=
              2) {
              Message.warning('审核日期跨度至多两年。')
            }
            return !(tool.dateDiff(val.auditTimeStartDate, val.auditTimeEndDate)
              .Y >= 2)
          }
        }),
      },
      slots: {
        default: (params, h) => {
          return params.row.auditTime ? params.row.auditTime.split(' ')[0] : ''
        }
      }
    },
    {
      title: '审核人',
      field: 'auditName',
      minWidth: 120
    },
    {
      title: '操作',
      fixed: 'right',
      minWidth: 150,
      align: 'center',
      slots: {
        default: (params, h) => {
          let btns = []
          btns.push({
            title: '查看',
            event: 'viewFun',
            data: params.row
          })
          btns.push({
            title: '流程信息',
            event: 'processFun',
            data: params.row
          })
          return table.cell.btns(_this, h, btns)
        }
      }
    }]
    if (_this.curState == '0') {
      if (_this.orderType == '认购') {
        this.all.push(...publicArr, ...p2, ...waitAuditArr)
      } else {
        this.all.push(...publicArr, ...p1, ...p2, ...waitAuditArr)
      }
    } else if (_this.curState == '1') {
      if (_this.orderType == '认购') {
        this.all.push(...publicArr, ...p2, ...passAuditArr)
      } else {
        this.all.push(...publicArr, ...p1, ...p2, ...passAuditArr)
      }
    } else if (_this.curState == '2') {
      if (_this.orderType == '认购') {
        this.all.push(...publicArr, ...p2, ...rejectAuditArr)
      } else {
        this.all.push(...publicArr, ...p1, ...p2, ...rejectAuditArr)
      }
    }
  }

}

export default Column